const SERVICE = require("../../lib/js/service.js");

Page({
  data:{
    companyInfo_val: "",
    region: ["","",""],   //区域范围
    region_val: null,
    customItem: "全部",   //默认项
    JobNature: ["全职","兼职"],   //工作性质
    JobNature_val: null,
    experience: ["不限","1-3年","3-6年","6年以上"],   //工作经验
    experience_val: null,
    education: ["不限","小学","初中","中专","高中","大专","本科","本科及以上"],   //学历
    education_val: null,
    SalaryRange: [[], []],    //薪资范围
    SalaryRange_val: null,
    SalaryRangeItem: [0,0],
    jobDescription_val: "",
    positionInfo:null,   //职位详情
    userInfo: wx.getStorageSync("userInfo")
  },

  onLoad(options){
    let td = this.data;
    for(let i = 0;i < 25; i ++){
      td.SalaryRange[0].push(i + 1);
      td.SalaryRange[1].push(i + 2 + 'k');      
    }
    this.setData({
      SalaryRange: td.SalaryRange,
      positionInfo: options.positionInfo ? JSON.parse(options.positionInfo) : null
    });
  },

  onShow(e){
    if(e){
      if(e.title == "公司信息"){
        this.setData({
          companyInfo_val: e.info
        });
      }else if(e.title == "职位描述"){
        this.setData({
          jobDescription_val: e.info
        });
      }
    }
  },

  /*分享*/ 
  onShareAppMessage(){
    
  },
  
  /*公司信息*/
  // companyInfo(e){
  //   let td = this.data;
  //   wx.navigateTo({
  //     url: "../compile-info/compile-info?title=公司信息&info=" + td.companyInfo_val,
  //   });
  // }, 
  
  /*获取范围*/
  bindRegionChange(e){
    let region_val = e.detail.value;
    this.setData({
      region_val: region_val[0] + '/' + region_val[1] + '/' + region_val[2]
    });
  },

  /*获取工作性质*/
  getJobNature(e){
    let td = this.data;
    this.setData({
      JobNature_val: td.JobNature[e.detail.value]
    });
  },

  /*获取工作经验*/
  getExperience(e){
    let td =  this.data;
    this.setData({
      experience_val: td.experience[e.detail.value]
    });
  },
   
  /*获取学历*/
  getEducation(e){
    let td = this.data;
    this.setData({
      education_val: td.education[e.detail.value]
    });
  }, 

  /*获取薪资范围*/
  getSalaryRange(e){
    let td = this.data;
    this.setData({
      SalaryRange_val: td.SalaryRange[0][e.detail.value[0]] + '~' + td.SalaryRange[1][e.detail.value[1]]
    });
  },

  /*获取薪资范围*/
  changeGetSalaryRange(e){
    let td = this.data, item = e.detail.value,  change_item = e.detail.column;
    if (change_item == 0){
      td.SalaryRange[1] = [];
      for (let i = (item + 2); i < 27; i++) {
        td.SalaryRange[1].push(i + 'k');
      }
      td.SalaryRangeItem[0] = item;
      td.SalaryRangeItem[1] = 0;                   
      this.setData({
        SalaryRange: td.SalaryRange,
        SalaryRangeItem: td.SalaryRangeItem
      });
    }
  },

  /*职位描述*/
  jobDescription(){
    let td = this.data;
   
    // wx.navigateTo({
    //   url: "../compile-info/compile-info?title=职位描述&info=" + 
    //   (td.jobDescription_val || td.positionInfo ? td.positionInfo.position_describe : ''),
    // });

    if (td.positionInfo) {
      wx.navigateTo({
        url: "../compile-info/compile-info?title=职位描述&info=" + td.positionInfo.position_describe
      });
    } else if (td.jobDescription_val){
      wx.navigateTo({
        url: "../compile-info/compile-info?title=职位描述&info=" + td.jobDescription_val
      });
    }else{
      wx.navigateTo({
        url: "../compile-info/compile-info?title=职位描述&info="
      });
    }
  }, 
  
  formSubmit(e){
    let form_obj = e.detail.value;
    if(!form_obj.ci){
      wx.showToast({
        title: "请填写公司信息",
        icon: "none",
        mask:true
      });
    } else if (!form_obj.pn) {
      wx.showToast({
        title: "请填写职位名称",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.site) {
      wx.showToast({
        title: "请填写工作地点",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.da) {
      wx.showToast({
        title: "请填写详细地址",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.jn) {
      wx.showToast({
        title: "请填写工作性质",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.experience) {
      wx.showToast({
        title: "请填写工作经验",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.education) {
      wx.showToast({
        title: "请填写学历",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.sr) {
      wx.showToast({
        title: "请填写薪资范围",
        icon: "none",
        mask: true
      });
    } else if (!form_obj.jd) {
      wx.showToast({
        title: "请填写职位描述",
        icon: "none",
        mask: true
      });
    }else{
      let td = this.data;
      if (td.positionInfo){
        this.editInfo(form_obj);
      }else{
        this.createRecruitment(form_obj);
      }
    }
  },

  /*发布*/
  createRecruitment(formObj){
    let td = this.data;
    SERVICE.POST(SERVICE.URL.createRecruitment, {
      thirdCookie: wx.getStorageSync("thirdCookie"),
      position_name: formObj.pn,
      job_address: formObj.site,
      detail_address: formObj.da,
      job_nature: formObj.jn,
      experience: formObj.experience,
      education: formObj.education,
      position_describe: formObj.jd,
      pay: formObj.sr,
      protocol_status: 1
    }, (status, res) => {
      if (res.data.status == "success") {
        wx.navigateBack();
      } else {
        wx.showToast({
          title: "发布失败",
          icon: "none",
          mask: true
        });
      }
    });
  }, 

  /*编辑*/
  editInfo(formObj){
    let td = this.data;
    SERVICE.POST(SERVICE.URL.editInfo, {
      edit_id: td.positionInfo.id,
      thirdCookie: wx.getStorageSync("thirdCookie"),
      position_name: formObj.pn,
      job_address: formObj.site,
      detail_address: formObj.da,
      job_nature: formObj.jn,
      experience: formObj.experience,
      education: formObj.education,
      position_describe: formObj.jd,
      pay: formObj.sr,
      protocol_status: 1
    }, (status, res) => {
      if (res.data.status == "success") {
        wx.navigateBack({
          delta: 2
        });
      } else {
        wx.showToast({
          title: "发布失败",
          icon: "none",
          mask: true
        });
      }
    });
  } 
});
